class Solution:
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        raw, col = len(grid), len(grid[0])
        newGrid = [[0]*(col+2)]
        for x in grid:
            newGrid.append([0]+x+[0])
        newGrid.append([0]*(col+2))
        count = 0
        for i in range(1, raw+2):
            for j in range(1, col+2):
                if newGrid[i][j] == 1:
                    if newGrid[i][j-1] == 0: count += 1
                    if newGrid[i-1][j] == 0: count += 1
                    if newGrid[i+1][j] == 0: count += 1
                    if newGrid[i][j+1] == 0: count += 1
        return count


data = [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]]
s = Solution()
print(s.islandPerimeter(data))